package farmer.update.building;

import farmer.core.Building;

public abstract class Farm extends Building{
	
	public static int woodCostS = 50;
	public static int rockCostS = 0;

	protected boolean isReadyToHarvest = false;
	private final int timeToGrow = 10*1000;
	private int timeUntilMature = timeToGrow;
	private boolean isSelectedByWorker = false;
	protected int ressource = 0;
	private int maxRessource = 3;
	int ressourceSelected = 0;
	
	public Farm(int x, int y) {
		super(x, y);
		size = 4;
		woodCost = 50;
		rockCost = 0;
	}
	
	public void setHarvested(){
		ressourceSelected--;
		ressource--;
		if(ressourceSelected == ressource){
			isReadyToHarvest = false;
		}else{
			isSelectedByWorker = false;
		}
		if(ressource == 2){
			timeUntilMature = timeToGrow;
		}
	}
	
	public void setWorkerSelect(){
		ressourceSelected++;
		if(ressourceSelected == ressource){
			isSelectedByWorker = true;
		}
	}
	
	public boolean isReadyToHarvest(){
		return isReadyToHarvest && !isSelectedByWorker;
	}
	
	@Override
	public void update(int deltaTime){
		if(timeUntilMature > 0){
			timeUntilMature -= deltaTime;
			if(timeUntilMature <= 0){;
				ressource++;
				isReadyToHarvest = true;
				isSelectedByWorker = false;
				if(ressource == maxRessource){
					timeUntilMature = 0;
				}else{
					timeUntilMature = timeToGrow;
				}
			}
		}
	}

}
